package com.sykuku.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sykuku.dao.DishFlavorMapper;
import com.sykuku.dao.DishMapper;
import com.sykuku.dto.DishDto;
import com.sykuku.pojo.Dish;
import com.sykuku.pojo.DishFlavor;
import com.sykuku.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
//继承Iservice的实现接口类就可以直接获取Iservice中的方法  再次实现定的service接口
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {


    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Override
    public Page<DishDto> dishPage(Integer page, Integer pageSize, String name) {
        log.info("业务层处理菜品管理分页查询数据为:{}{}{}",page,pageSize,name);
        Page<DishDto> pages = new Page<>(page,pageSize);

        return baseMapper.dishPage(pages, name);
    }

    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {

        //保存菜品的基本信息到菜品表dish
        save(dishDto);
        //获得dish的id属性赋值给口味的dishid
        Long id = dishDto.getId();
        //循环遍历设置dishid后添加进口味表中
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors.stream().forEach((flavor) ->{
            flavor.setDishId(id);
            //保存菜品口味数据到菜品口味表dish_flavor,先用接口的方法
            dishFlavorMapper.insert(flavor);
        });
        //这是统一保存的方法dishFlavorService.saveBatch(flavors);
    }

    //套餐管理修改回显界面
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，从dish表查询
        Dish dish = getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(lqw);
        dishDto.setFlavors(dishFlavors);

        return dishDto;

    }
    //套餐管理修改套餐信息
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {

        //更新dish表基本信息
        updateById(dishDto);
        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        Long id = dishDto.getId();
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,id);
        dishFlavorMapper.delete(queryWrapper);

        //添加当前提交过来的口味数据---dish_flavor表的insert操作

        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().forEach(flavor ->{
            flavor.setDishId(id);
            dishFlavorMapper.insert(flavor);
        } );
        //这是统一保存的方法dishFlavorService.saveBatch(flavors);
    }
}
